package kz.pompei.OneGwt.client.personrole.impl;

import kz.pompei.OneGwt.client.personrole.PersonListMed;
import kz.pompei.OneGwt.client.personrole.PersonroleManagerMed;
import kz.pompei.OneGwt.client.personrole.PersonroleManagerView;
import kz.pompei.OneGwt.client.personrole.RoleListMed;
import kz.pompei.OneGwt.ifaces.util.Handler1;
import kz.pompei.OneGwt.remote.PersonroleManagerServiceAsync;

public class PersonroleManagerMedImpl implements PersonroleManagerMed {
  
  @SuppressWarnings("unused")
  private PersonroleManagerView view;
  @SuppressWarnings("unused")
  private PersonroleManagerServiceAsync service;
  private PersonListMed personListMed;
  private RoleListMed roleListMed;
  private boolean initialized = false;
  
  public void setView(PersonroleManagerView view) {
    this.view = view;
  }
  
  public void setService(PersonroleManagerServiceAsync service) {
    this.service = service;
  }
  
  public void setPersonListMed(PersonListMed personListMed) {
    this.personListMed = personListMed;
  }
  
  @Override
  public void refresh() {
    initialize();
    personListMed.refresh();
    roleListMed.refresh();
  }
  
  private void initialize() {
    if (initialized) return;
    defineEventHandlers();
    initialized = true;
  }
  
  private void defineEventHandlers() {
    personListMed.addPersonSelectHandler( new Handler1<String>() {
      @Override
      public void handle(String personId) {
        roleListMed.setPersonId( personId );
      }
    } );
  }
  
  public void setRoleListMed(RoleListMed roleListMed) {
    this.roleListMed = roleListMed;
  }
  
}
